using System;
using System.IO;
using System.Collections;

namespace MSHelpLib.BaseCode
{
	/// <summary>
	/// Summary description for IndexItem2.
	/// </summary>		
	public class IndexItem2
	{
        /// <summary>
        /// 
        /// </summary>
		public string		Title="";
		private string		m_Url="";
        /// <summary>
        /// 
        /// </summary>
		public int			Indent=0;
        /// <summary>
        /// 
        /// </summary>
		public bool			bFlag=false;
        /// <summary>
        /// 
        /// </summary>
		public ArrayList	Children=new ArrayList();

		// [NonSerialized]
        /// <summary>
        /// 
        /// </summary>
		public IHelpFile	helpFile=null;

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexItem2"/> class.
        /// </summary>
        /// <param name="aTitle">A title.</param>
        /// <param name="aURL">A URL.</param>
        /// <param name="aHelpFile">A help file.</param>
        /// <param name="aIndent">A indent.</param>
		public IndexItem2(string aTitle, string aURL, IHelpFile aHelpFile, int aIndent)
		{			
			Title=aTitle;
			Url=aURL;
			helpFile=aHelpFile;
			Indent=aIndent;
		}

        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
		public string Url
		{
			set
			{
				m_Url=value;
			}
			get
			{			
				return m_Url;
			}
		}

        /// <summary>
        /// Gets the full URL.
        /// </summary>
        /// <value>The full URL.</value>
		public string FullURL
		{
			get
			{
				string sL = m_Url;

				if( (sL.ToLower().IndexOf("http://") >= 0) ||
					(sL.ToLower().IndexOf("https://") >= 0) ||
					(sL.ToLower().IndexOf("mailto:") >= 0) ||
					(sL.ToLower().IndexOf("ftp://") >= 0) || 
					(sL.ToLower().IndexOf("file://") >= 0) || 
					(sL.ToLower().IndexOf("ms-its:") >= 0))
					return sL;

				// Remove Leading Slash
				if ((sL!=null) && (sL.Length>2) && ((sL.StartsWith("/")) || (sL.StartsWith(@"\"))))
					sL=sL.Substring(1);

				if (helpFile!=null)
					return this.helpFile.UrlPrefix + helpFile.FileName + "::/" + sL;
				else
					return  "::/" + sL;
			}
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexItem2"/> class.
        /// </summary>
        /// <param name="aTitle">A title.</param>
		public IndexItem2(string aTitle)
		{			
			Title=aTitle;		
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="IndexItem2"/> class.
        /// </summary>
		public IndexItem2()
		{					
		}

        /// <summary>
        /// Gets the indent.
        /// </summary>
        /// <param name="indent">The indent.</param>
        /// <returns></returns>
		private string GetIndent(int indent)
		{
			string sRet = "";

			for(int i=0; i<indent; i++)
				sRet += "   ";

			return sRet;
		}

        /// <summary>
        /// Gets the index title.
        /// </summary>
        /// <value>The index title.</value>
		public string IndexTitle
		{
			get
			{
				return GetIndent(this.Indent)+this.Title;
			}
		}

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
		public override string ToString()
		{
			return Title+":"+Url+":"+Children.Count.ToString();
		}

        /// <summary>
        /// Saves to stream.
        /// </summary>
        /// <param name="st">The st.</param>
		public void SaveToStream(BinaryWriter st)
		{		
			st.Write(this.m_Url);
			st.Write(this.Title);
			st.Write((Int32)this.Children.Count);
			foreach(IndexItem2 item in this.Children)
			{
				item.SaveToStream(st);
			}
		}

        /// <summary>
        /// Loads from stream.
        /// </summary>
        /// <param name="st">The st.</param>
        /// <param name="helpFile">The help file.</param>
		public void LoadFromStream(BinaryReader  st, IHelpFile helpFile)
		{			
			this.helpFile=helpFile;
			this.m_Url=st.ReadString();
			this.Title=st.ReadString();
			int ChildCount=st.ReadInt32();
			for(int i=0;i<ChildCount;i++)
			{
				IndexItem2 item=new IndexItem2();				
				Children.Add(item);
				item.LoadFromStream(st,helpFile);
			}
		}
	}

    /// <summary>
    /// 
    /// </summary>
	public class SortIndexItems : IComparer  
	{			
		int IComparer.Compare( Object x, Object y )  
		{
			IndexItem2 item1=(IndexItem2)x;
			IndexItem2 item2=(IndexItem2)y;				
			return( (new CaseInsensitiveComparer()).Compare( item1.Title, item2.Title ) );
		}
	}

    /// <summary>
    /// 
    /// </summary>
	public class SortIndexItemsURL : IComparer  
	{			
		int IComparer.Compare( Object x, Object y )  
		{
			IndexItem2 item1=(IndexItem2)x;
			IndexItem2 item2=(IndexItem2)y;				
			return( (new CaseInsensitiveComparer()).Compare( item1.FullURL, item2.FullURL) );
		}
	}
}
